package cn.edu.gdmec.android.broadcastbestpractice;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.WindowManager;

/**
 * Created by Jack on 2017/12/21.
 */

public class ForceOfflineReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(final Context context, Intent intent){
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder ( context );
        dialogBuilder.setTitle ( "Warning" );
        dialogBuilder.setMessage ( "You are forced to be offline.Please try to login again." );
        dialogBuilder.setCancelable ( false );
        dialogBuilder.setPositiveButton ( "OK",
                new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which){
                        ActivityCollector.finishAll ();
                        Intent intent = new Intent ( context, LoginActivity.class );
                        intent.addFlags ( Intent.FLAG_ACTIVITY_NEW_TASK );
                        context.startActivity ( intent );
                    }
                });
        AlertDialog alertDialog = dialogBuilder.create ();
        alertDialog.getWindow ().setType ( WindowManager.LayoutParams.TYPE_SYSTEM_ALERT );
        alertDialog.show ();
    }
}
